import Vue from "vue";
import VueRouter from "vue-router";
import store from '@/store'
import { getItem } from '@/utils/storage'
Vue.use(VueRouter);

const routes = [
  {
    path: "/footprint",
    name: "Footprint",
    component: () => import("@/views/Login/component/footprint"),
  },
  {
    path: "/collect",
    name: "Collect",
    component: () => import("@/views/Login/component/collect"),
  },
  {
    path: '/subject/detail/:id',
    name: 'subDetail',
    component: () => import('@/views/Subject/detail')
  },

  {
    path: '/message/:id',
    name: 'message',
    component: () => import('@/views/Subject/message')
  },
  {
    path: '/add/comment/:id',
    name: 'addcomment',
    component: () => import('@/views/Subject/add-comment')
  },
  {
    path: "/login",
    name: "Login",
    component: () => import("@/views/Login"),
  },
  {
    path: "/sign",
    name: "sign",
    component: () => import("@/views/Login/component/sign"),
  },
  {
    path: "/reset",
    name: "reset",
    component: () => import("@/views/Login/component/reset"),
  },
  {
    path: "/bddetail/:bddetailId",
    name: "bddetail",
    component: () => import("@/views/Home/components/bddetail"),
  },
  {
    path: "/brand",
    name: "brand",
    component: () => import("@/views/Home/components/brand"),
  },
  {
    path: "/newgoods",
    name: "newgoods",
    component: () => import("@/views/Home/components/newgoods"),
  },
  {
    path: "/hotgoods",
    name: "hotgoods",
    component: () => import("@/views/Home/components/hotgoods"),
  },
  {
    path: '/goods/:goodsId',
    name: 'goods',
    meta: {
      keepAlive: true
    },
    component: () => import('@/views/Classify/components/goods.vue')
  },
  {
    path: '/goodsDetail/:detailId',
    name: 'goodsDetail',
    component: () => import('@/views/Classify/components/goodsDetail.vue')
  },
  {
    path: '/paymentOrder/:id',
    component: () => import('@/views/Shopping/paymentOrder')
  },
  {
    path: '/checkout',
    component: () => import('@/views/Shopping/checkout'),
    meta: {
      keepAlive: true  // 需要缓存
    }
  },
  {
    path: "/",
    component: () => import("@/views/Layout"),
    // meta: {
    //   // keepAlive: true  // 需要缓存
    // },
    children: [
      {
        path: "",
        component: () => import("@/views/Home"),
      },
      {
        path: "subject",
        component: () => import("@/views/Subject"),
      },
      {
        path: "classify",
        component: () => import("@/views/Classify"),
      },
      {
        path: "shopping",
        component: () => import("@/views/Shopping"),
      },
      {
        path: "user",
        component: () => import("@/views/User"),
      },
    ],
  },
  {
    path: "/search",
    name: "Search",
    component: () => import("@/views/Search"),
  },
  {
    path: "/orders",
    name: "Orders",
    component: () => import("@/views/orders"),
  },
  {
    path: "/address",
    name: "Address",
    component: () => import("@/views/address"),
  },
  {
    path: '/editAddress',
    name: 'editAddress',
    component: () => import('@/views/editAddress')
  },
  {
    path: '/editAddress1/:id',
    name: 'editAddress1',
    component: () => import('@/views/editAddress')
  },
];

const router = new VueRouter({
  // scrollBehavior(to, from, savedPosition) {
  //   console.log(to);
  //   console.log(from);
  //   console.log(savedPosition);
  //   // return { x: 0, y: 300 }
  // },
  routes,
});

// 导航守卫函数
router.beforeEach((to, from, next) => {
  // 要去的页面是否需要登录呢？
  let authList = ['/shopping', '/orders', '/collect', '/footprint', '/address']
  // console.log('to.path', to.path);
  if (authList.includes(to.path)) {
    // console.log(111);
    // ==>A、需要登录，判断是否登录了
    // let {__ob__} = store.getters.token
    // console.log(__ob__.dep);
    const data = getItem('TOUTIAO_USER')
    store.commit('setUser', data)
    // console.log('2222222222222222', store.getters.token);
    if (store.getters.token) {
      // =====> 1. 登录了，放行
      console.log(store.getters.token);
      next()
    } else {
      // =====> 2. 未登录了，跳转到登录界面
      next('/login?to=' + to.path)
    }
  } else {
    // ==>B、不需要登录，直接放行
    next()
  }
})

export default router;
